﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Sol_NetCoreReadConfig.Model;
using Sol_NetCoreReadConfig.Settings;

namespace Sol_NetCoreReadConfig.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        public IConfiguration Configuration { get; }
        public section2 Section2 { get; }
        public AppSettings appSettings { get; }

        public ValuesController(
            IConfiguration _configuration,
            IOptions<section2> _section1,
            AppSettings _appSettings)
        {
            Configuration = _configuration;
            Section2 = _section1.Value;
            appSettings = _appSettings;
        }

        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            var list = new List<string>() {
             Configuration.GetConnectionString("Default"),
             Configuration.GetConnectionString("IP"),
             Configuration.GetConnectionString("IP2"),
             Configuration.GetConnectionString("IP3"),
             Configuration["section0:key0"],
             Configuration["section0:key1"],
             Configuration["section0:LogLevel:Default"],
             Configuration.GetSection("section1")["key0"],
             Configuration.GetSection("section1")["key1"]
            };

            //通过 Program.cs中 注入获取值
            var obj1 = Section2;
            list.Add(obj1.key2);
            list.Add(obj1.key3);

            //不通过注入赋值
            var obj2 = new section2();
            Configuration.GetSection("section2").Bind(obj2);
            var obj3 = Configuration.GetSection("section2").Get<section2>();


            //对应Program.cs的arrayDict
            var ArrayExample = Configuration.GetSection("array").Get<ArrayExample>();

            //在 Program.cs 中的添加对应json,xml 配置文件
            var starship = new Starship();
            Configuration.GetSection("starship").Bind(starship);

            var TvShow = Configuration.GetSection("tvshow").Get<TvShow>();
            var JsonArrayExample = Configuration.GetSection("json_array").Get<JsonArrayExample>();

            //自定义的 AppSettings 在 Startup.cs 全局注入的
            var AAA = new AppHostSetting();
            var BBB = appSettings.App;
            var CCC = appSettings.Ldap;
            return list;
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<IEnumerable<string>> Get(int id)
        {
            var list = new List<string>() {
            ConfigurationManager.AppSettings["a"],
            ConfigurationManager.AppSettings["b"],
            ConfigurationManager.AppSettings["c"],
            ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString,
            ConfigurationManager.ConnectionStrings["MSSQL"].ProviderName,
            ConfigurationManager.ConnectionStrings["MYSQL"].ConnectionString,
            ConfigurationManager.ConnectionStrings["MYSQL"].ProviderName,
            };

            return Ok(list);
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        [HttpGet("InAsyncc")]
        public async Task<IActionResult> InAsyncc()
        {
            var dic = await Task.FromResult(142);
            return Ok(dic);
        }

    }
}
